/* Universidade Federal de Campina Grande - UFCG
 * Centro de Engenharia El�trica e Inform�tica - CEEI
 * Departamento de Sistemas e Computa��o - DSC
 * Curso: Ci�ncia da Computa��o
 * Disciplina: Desenvolvimento de Aplica��es Corporativas Avan�adas (2008.1)
 * Professor: Jacques Philippe Sauv�
 * Monitor: Guilherme Mauro Germoglio Barbosa
 * Alunos: Daniel Souza Coelho
 * 		   Everton Leandro Galdino Alves
 * 		   Odilon Francisco de Lima Junior	
 */
package lembrete.core.beans;


import java.util.Date;
import java.util.Properties;

//import org.apache.log4j.BasicConfigurator;
//import org.apache.log4j.FileAppender;
//import org.apache.log4j.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * Essa classe eh responsavel por enviar os emails para os usuarios do sistema. <br> 
 * 
 * @author Everton Leandro <br>
 *         Odilon Francisco <br>
 *         Daniel Coelho <br>
 * @version 1.0 
 */
public class MailSender {

	private Mail mail;
	
	private String user;
	private String password;
	
	private String host;
	private String from;
	private InternetAddress[] toList;

	//private FileAppender appender;
	//Logger log = Logger.getLogger("com.mobilefish.Log4jDemo");

	/**
	 * Metodo criador da classe MailSender 
	 */
	public MailSender(InternetAddress[] toList, Mail mail){
		//BasicConfigurator.configure();
		this.host = "smtp.gmail.com"; 
		this.from = "Lembrete <sistema.lembrete.daca@gmail.com>";
		this.toList = toList;
		this.mail = mail;		
		this.user = "sistema.lembrete.daca";
		this.password = "dacalembrete";
	}
	
	/**
	 * Metodo que envia email
	 */
	public void sendMail(){

		try{
			Properties props = System.getProperties();  
			props.put("mail.smtp.auth", "true");  
			props.put("mail.smtp.starttls.enable","true"); 
			
			Session session = Session.getDefaultInstance(props);  
			MimeMessage msg = new MimeMessage(session);  
			
			msg.setFrom(new InternetAddress(from));  
			msg.setRecipients(Message.RecipientType.TO, toList);  
			msg.setSentDate(new Date());  
			msg.setSubject(mail.getSubject());  
			msg.setText(mail.getContent());  
			msg.saveChanges();  
			
			Transport transport = session.getTransport("smtp");  
			transport.connect(host, user, password);  
			transport.sendMessage(msg, msg.getAllRecipients());  
			transport.close();  

			//log.info("This is my info message.");
		} catch (MessagingException e){
			//log.warn("This is my warn message.");
		}

	}
	
}